#include "ctradebymarketdata.h"
#include "UFX/mdUFX.h"
#include "UFX/traderUFX.h"
#include"ufxconfig.h"

CThreadSafeValue CQueryInfo::currRequestId(1);
int CQueryInfo::NewRequestId()
{
    return currRequestId.Increase();
}

CTradeByMarketData::CTradeByMarketData()
{
    lp_TraderRequest = new CTraderRequestMode(this);
    lp_MDRequest = new CMdRequestMode(this);

    b_SendOrder = false;
    en_last_price = 0;
    l_update_time = 0;
    l_trade_amount = 0;
}

CTradeByMarketData::~CTradeByMarketData()
{
    delete(lp_TraderRequest);
    delete(lp_MDRequest);
}

int CTradeByMarketData::Init(QString UserName,QString Password)
{
    m_strFundAccount = UserName.toStdString();
    m_strPassWord = Password.toStdString();
    c_EntrustType = '7';
    m_strTraderAddr = UFXConfig::getInstance()->GetString("config","TraderAddr","").toStdString();
    m_strMdAddr = UFXConfig::getInstance()->GetString("config","MdAddr","").toStdString();

    //begin 获得本机信息，uft2.0中331100中必须传op_station
    char mac[128];
    char ip[64];
    if(strcmp(m_strTraderAddr.c_str(), "") == 0 || GetLocalMACIP(mac,ip,m_strTraderAddr.c_str()) == false)
    {
        //ShowMessage("网络配置无效");
        return -1;
    }
    m_localip = ip;
    m_localMac = mac;
    m_opStation = m_localip +":"+ m_localMac;
	this->lp_TraderRequest->m_opStation = m_opStation;
    //end zhoukm

    string m_TraderName = NewClientName("Trader", mac);
    if(lp_TraderRequest->InitConn(m_TraderName.c_str(), "license.dat", m_strTraderAddr.c_str()) != 0)
    {
        //cerr<<"交易地址："<<m_strTraderAddr<<"-连接失败..."<<endl;
        return 0;
    }

    string m_MDName = NewClientName("MD", mac);
    if(lp_MDRequest->InitConn(m_MDName.c_str(), "license.dat", m_strMdAddr.c_str()) != 0)
    {
        //cerr<<"行情地址："<< m_strMdAddr <<"-连接失败..."<<endl;
        return 0;
    }

    //交易登录
    lp_TraderRequest->ReqFunction331100(m_strFundAccount.c_str(), m_strPassWord.c_str(), c_EntrustType);
    //行情登录
    lp_MDRequest->ReqFunction331100(m_strFundAccount.c_str(), m_strPassWord.c_str(), c_EntrustType);

    Sleep(1000);

    //processTrd.Start(Process,0,this);
    //if(m_conf.c_TestMode == '1')
        //processTest.Start(Process_Test,0,this);

    return 0;
}

int CTradeByMarketData::Join()
{
    return 0;
}

CTraderRequestMode* CTradeByMarketData::GetTraderRequest()
{
    return lp_TraderRequest;
}

CMdRequestMode*     CTradeByMarketData::GetMdRequest(){
    return lp_MDRequest;
}

//订阅行情
bool CTradeByMarketData::SubscribeMarketData(QString ContractName){
    return lp_MDRequest->SubFunction33100(ContractName.toStdString());
}
bool CTradeByMarketData::OrderInsert(const char* exch_type, const char* contract_code, char c_bs, char c_direction, char c_hedge, int l_entrust_amount, double en_entrust_price,const char* entrust_prop){
    return lp_TraderRequest->ReqFunction338202(exch_type,contract_code,c_bs,c_direction,c_hedge,l_entrust_amount,en_entrust_price,entrust_prop);
}
bool CTradeByMarketData::CancelOrder(const char* exch_type, int entrust_no){
	return lp_TraderRequest->ReqFunction338217(exch_type,entrust_no);
}
//338300 期货客户资金查询
bool CTradeByMarketData::QryTradingAccount(){
    return lp_TraderRequest->ReqFunction338300();
}
//330851 查询合约信息
bool CTradeByMarketData::QryContractsInfo(QString contractname, const char* positionstr){
    return lp_TraderRequest->ReqFunction330851("",contractname.toStdString().c_str(),positionstr);
}
//338303 查询期货持仓信息
bool CTradeByMarketData::QryHoldsInfo(QString positionstr){
    return lp_TraderRequest->ReqFunction338303(positionstr);

}
//338305 查询合约保证金信息
bool  CTradeByMarketData::QryInstrumentMarginRate(QString contractcode,const char* position_str){
    return lp_TraderRequest->ReqFunction338305(contractcode,position_str);
}

//338302 查询期货成交信息
bool CTradeByMarketData::QryTradeRecs(QString positionstr){
    return lp_TraderRequest->ReqFunction338302(positionstr.toStdString().c_str());
}

//339853 查询期货历史成交信息
bool CTradeByMarketData::QryHisTradeRecs(QString start,QString end,QString positionstr){
    return lp_TraderRequest->ReqFunction339853(start,end,positionstr.toStdString().c_str());
}
